package com.example.springsecurity.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.StudentMustScoreSubmit;
import com.example.springsecurity.domain.SysUser;
import com.example.springsecurity.domain.U3dResult;
import com.example.springsecurity.security.SecurityUtils;
import com.example.springsecurity.service.StudentExperimentService;
import com.example.springsecurity.utils.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping(value = "/student/experiment")
public class StudentExperimentController {

    private Logger logger= LoggerFactory.getLogger(StudentExperimentController.class);


    @Autowired
    public StudentExperimentService studentExperimentService;



    /**
     * 学生用户有条件筛选全部实验
     * @param searchAllExperiment
     * @return
     */
    @RequestMapping(value = "/searchAllExperiment",method = RequestMethod.POST)
    public ResultBean searchAllExperiment(@RequestBody String searchAllExperiment){
        JSONObject jsonObject=JSONObject.parseObject(searchAllExperiment);
        Integer collegeId=jsonObject.getInteger("collegeId");
        Integer majorId=jsonObject.getInteger("majorId");
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        logger.info("传参{}",searchAllExperiment);
        return studentExperimentService.searchAllExperiment(collegeId,majorId,projectName,pageNum,pageSize);
    }


    /**
     * 学生用户通过项目编号来获取项目的部分信息
     * @param getExperimentDetail
     * @return
     */
    @RequestMapping(value = "/getExperimentDetail",method = RequestMethod.POST)
    public ResultBean getExperimentDetail(@RequestBody String getExperimentDetail){
        JSONObject jsonObject= JSON.parseObject(getExperimentDetail);
        Integer projectId=jsonObject.getInteger("projectId");
        return studentExperimentService.getExperimentDetail(projectId);
    }


    /**
     * 开始实验
     * @param data
     * @return
     */
    @RequestMapping(value = "/doExperiment",method = RequestMethod.POST)
    public ResultBean doExperiment(@RequestBody String data){
        SysUser sysUser=SecurityUtils.getLoginUser().getSysUser();
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId(1L);
        JSONObject jsonObject=JSON.parseObject(data);
        Integer projectId=jsonObject.getInteger("projectId");
        Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        Integer assignId=jsonObject.getInteger("assignId");
        return studentExperimentService.doExperiment(projectId,status,sysUser,assignId);
    }


    /**
     * 获取到自己选修或者必修的实验报告
     * @param data
     * @return
     */
    @RequestMapping(value = "/getSubmitContent",method = RequestMethod.POST)
    public ResultBean getSubmitContent(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        // Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        Integer submitId=jsonObject.getInteger("submitId");
        return studentExperimentService.getSubmitContent(submitId);
        // return studentExperimentService.getSubmitContent(submitId,status);
    }


    /**
     * 学生用户获取到视频学习的数据
     * @param getExperimentVideo
     * @return
     */
    @RequestMapping(value = "/getExperimentVideo",method =RequestMethod.POST)
    public ResultBean getExperimentVideo(@RequestBody String getExperimentVideo){
        JSONObject jsonObject=JSON.parseObject(getExperimentVideo);
        Integer projectId=jsonObject.getInteger("projectId");
        Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        return studentExperimentService.getExperimentVideo(projectId);
    }


    /**
     * 提交视频成绩（status 为0的话表示是选修的成绩提交编号，为1的话是必修的成绩提交编号）
     * @param data
     * @return
     */
    @RequestMapping(value = "/submitVideoScore",method = RequestMethod.POST)
    public ResultBean submitVideoScore(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Boolean videoStatus=jsonObject.getBoolean("videoStatus");
        Integer submitId=jsonObject.getInteger("submitId");
        Integer status=jsonObject.getInteger("status");
        return studentExperimentService.submitVideoScore(submitId,videoStatus,status);
    }


    /**
     * 学生用户获取到虚拟实验地址，包括（1为链接实验，2为客户端实验，3为演示实验；0为嵌套，1为跳转；文件名称；上传文件地址）
     * @param getAddressLink
     * @return
     */
    @RequestMapping(value = "/getAddressLink",method = RequestMethod.POST)
    public ResultBean getAddressLink(@RequestBody String getAddressLink ){
        JSONObject jsonObject=JSON.parseObject(getAddressLink);
        Integer submitId=jsonObject.getInteger("submitId");//成绩记录编号
        Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        return studentExperimentService.getAddressLink(submitId,status);
    }


    /**
     * 学生用户获取到实验报告（主要是返回项目的实验要求）
     * @param getExperimentReportContent
     * @return
     */
    @RequestMapping(value = "/getExperimentReportContent",method = RequestMethod.POST)
    public ResultBean getExperimentReportContent(@RequestBody String getExperimentReportContent){
        JSONObject jsonObject=JSON.parseObject(getExperimentReportContent);
        Integer projectId=jsonObject.getInteger("projectId");
        return studentExperimentService.getExperimentReportContent(projectId);
    }


    /**
     * 学生用户完成虚拟实验操作 U3D传递值过来(submitId,status,keys数组和values数组)
     * @param u3dResult
     * @return
     */
    @RequestMapping(value = "/updateVirtualExperiment",method = RequestMethod.POST)
    public ResultBean updateVirtualExperiment(@RequestBody U3dResult u3dResult){
        Integer submitId=u3dResult.getSubmitId();
        Integer status=u3dResult.getStatus();
        Integer virtualExperimentScore=u3dResult.getVirtualExperimentScore();
        String[] keys=u3dResult.getKeys();
        String[] values=u3dResult.getValues();
        logger.info("keys的值为{}",keys.toString());
        logger.info("values的值为{}",values.toString());
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i <keys.length ; i++) {
            sb.append(keys[i]).append(":").append(values[i]).append(";");
        }
        return studentExperimentService.updateVirtualExperiment(submitId,virtualExperimentScore,status,sb.toString());
    }


    /**
     * 学生保存作业记录submitStatus:0为暂存，1为提交
     * @param submitExperiment
     * @return
     */
    @RequestMapping(value = "/submitExperiment",method = RequestMethod.POST)
    public ResultBean submitExperiment(@RequestBody String submitExperiment){
        JSONObject jsonObject=JSON.parseObject(submitExperiment);
        Integer submitId=jsonObject.getInteger("submitId");
        String experimentContent=jsonObject.getString("experimentContent");
        Integer submitStatus=jsonObject.getInteger("submitStatus");
        return studentExperimentService.submitExperiment(submitId,experimentContent,submitStatus);
    }


    /**
     * 学生用户我的实验（选修）有条件筛选
     * @param getStudentOptionalExperiment
     * @return
     */
    @RequestMapping(value = "/getStudentOptionalExperiment",method = RequestMethod.POST)
    public ResultBean getStudentOptionalExperiment(@RequestBody String getStudentOptionalExperiment){
        SysUser sysUser=SecurityUtils.getLoginUser().getSysUser();
        JSONObject jsonObject=JSON.parseObject(getStudentOptionalExperiment);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        return studentExperimentService.getStudentOptionalExperiment(pageNum,pageSize,projectName,sysUser.getUserId().intValue());
    }


    /**
     * 学生用户获取到之前做共享（选修）实验的记录信息
     * @param getStudentExperimentRecord
     * @return
     */
    @RequestMapping(value = "/getStudentExperimentRecord",method = RequestMethod.POST)
    public ResultBean getStudentExperimentRecord(@RequestBody String getStudentExperimentRecord ){
        JSONObject jsonObject=JSON.parseObject(getStudentExperimentRecord);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer projectId=jsonObject.getInteger("projectId");
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId(1L);
        return studentExperimentService.getStudentExperimentRecord(pageNum,pageSize,sysUser.getUserId(),projectId);
    }


    /**
     * 我的实验根据提交编号来获取到共享实验的成绩信息
     * @param data
     * @return
     */
    @RequestMapping(value = "/getExperimentShareRecord",method = RequestMethod.POST)
    public ResultBean getExperimentShareRecord(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer submitId=jsonObject.getInteger("submitId");
        Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        return studentExperimentService.getExperimentShareRecord(submitId,status);
    }


    /**
     * 根据状态和提交编号来获取到必修或者选修的成绩记录
     * @param data
     * @return
     */
    @RequestMapping(value = "/getExperimentSubmit",method = RequestMethod.POST)
    public ResultBean getExperimentSubmit(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer submitId=jsonObject.getInteger("submitId");
        Integer status=jsonObject.getInteger("status");//0为选修，1为必修
        return studentExperimentService.getExperimentSubmit(status,submitId);
    }


    /**
     * 学生用户根据学习状态获取到必修实验的信息，有条件筛选（实验名称）
     * @param getStudentMustExperiment
     * @return
     */
    @RequestMapping(value = "/getStudentMustExperiment",method = RequestMethod.POST)
    public ResultBean getStudentMustExperiment(@RequestBody String getStudentMustExperiment){
        JSONObject jsonObject=JSON.parseObject(getStudentMustExperiment);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        Integer accomplishCondition=jsonObject.getInteger("accomplishCondition");
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
//        SysUser sysUser=new SysUser();
//        sysUser.setUserId(2L);
        return studentExperimentService.getStudentMustExperiment(sysUser,projectName,accomplishCondition,pageNum,pageSize);
    }




    /**
     * 学生必修实验保存作业记录submitStatus:0为暂存，1为提交
     * @param studentMustScoreSubmit
     * @return
     */
    @RequestMapping(value = "/submitMustExperiment",method = RequestMethod.POST)
    public ResultBean submitMustExperiment(@RequestBody StudentMustScoreSubmit studentMustScoreSubmit){
        return studentExperimentService.submitMustExperiment(studentMustScoreSubmit);
    }


    /**
     * 撤销提交(根据实验布置编号)
     * @param revocationCorrect
     * @return
     */
    @RequestMapping(value = "/revocationCorrect",method = RequestMethod.POST)
    public ResultBean revocationCorrect(@RequestBody String revocationCorrect){
        JSONObject jsonObject=JSON.parseObject(revocationCorrect);
        Integer assignId=jsonObject.getInteger("assignId");
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
        return studentExperimentService.revocationCorrect(assignId,sysUser.getUserName());
    }

    /**
     * 更新共享或者必修的开始时间
     * @param data
     * @return
     */
    @RequestMapping(value = "/updateSubmitDate",method = RequestMethod.POST)
    public ResultBean updateSubmitDate(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer status=jsonObject.getInteger("status");
        Integer submitId=jsonObject.getInteger("submitId");//0为选修，1为必修
        return studentExperimentService.updateSubmitDate(status,submitId);
    }

}
